<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/templates/common/taglib.jsp"%>
<%@include file="/WEB-INF/templates/common/common.jsp"%>
<!DOCTYPE HTML>
<html>
  <head>
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <script  type="text/javascript" src="${basePath}/static/layer/layer.js"></script>
<link href="${basePath}/static/layer/layer_style.css" rel="stylesheet" type="text/css">
    <style>
      @media print {
      .paging{page-break-after:always}
      }
    </style>
    <script type="text/javascript">
    
    function getFiles(){
    	//var medicalOnlyId=location.search.split("=")[1];
    	var medicalOnlyId=$("#medicalOnlyId").val();
    	var applyReason=$("#applyReason").val();
    
    	var arr=[];
    	$.ajax({
    		type:"post",
    		url:"${basePath}/print/getUrlListByMedRecord",
    		data:{medicalOnlyId:medicalOnlyId,applyReason:applyReason},
    		success:function(data){
    			if(data!=null && data!=""){
    				for(var i in data){
    					//alert(JSON.stringify(data[i]));
    					arr.push(data[i]);
    				}
    			}else{
    				layer.alert("打印数据为空!", {icon: 5});
    				return;
    			}
    			//对数据进行分组
    			filelist=TwoArr(arr);
    			//开始打印
    			docReady();
    		}
    	})
    	
    	return arr;
    }
    
/*     var files= 
        
        [  
       {src : "${basePath}/static/upload/file.png", loaded : false,type:'pic'},
         {src : "${basePath}/static/upload/file.png", loaded : false,type:'pic'},
         {src : "${basePath}/static/upload/file.png", loaded : false,type:'pic'},
         {src : "${basePath}/static/upload/file.png", loaded : false,type:'pic'},  
         {src : "${basePath}/static/upload/file.png", loaded : false,type:'pic'} ,
          {src : "${basePath}/static/upload/clickshare.pdf", loaded : false,type:'pdf'} ,
           {src : "${basePath}/static/upload/clickshare.pdf", loaded : false,type:'pdf'}
        ]; */
    
    //生成二维数组
    function TwoArr(arr){
        var Arr=[];
        for(var i=0;i<arr.length;i++){
            var type1=arr[i].type;
            var newArr=[];
            if(type1=="pdf"){
                newArr[newArr.length]=arr[i];
                Arr[Arr.length]=newArr;
                continue;
            }else if(type1=="pic"){
                newArr[newArr.length]=arr[i];
                //判断当前元素是否是数组最后一个元素
                if(i==(arr.length-1)){
                    Arr[Arr.length]=newArr;
                    break;
                }
                //判断下一元素是否和前一个元素类型相同
                for(var j=i+1;j<arr.length;j++){
                    var type2=arr[j].type;
                    if(type2=="pdf"){
                        i=j-1;
                        Arr[Arr.length]=newArr;
                        break;
                    }else if(type2=="pic"){
                        newArr[newArr.length]=arr[j];
                        if(j==(arr.length-1)){
                            i=j;
                            Arr[Arr.length]=newArr;
                            break;
                        }
                    }

                }
            }
        }
        
        
        return Arr;
    }
    

    
   // var arrLength=filelist.length;

    function printWindow() {
      var allOk = true;
      $("img.for_print").each(function(i, v) {
        allOk = allOk && v.complete;
        return allOk;
      });
      if(allOk) {
        console.log("all img load ok");
        var contentDiv = $("div#content").focus();
        window.print();
        setTimeout(docReady, 150);
      }
      else {
        setTimeout(printWindow, 10);
        console.log("waiting all img load");
      }
    }
    
    function imgCounter(imgList) {
      this.total = imgList.length;
      this.current = 0;
      this.increase = function() {
        current += 1;
        console.log(this.src);
        console.log(current);
        if(current == total) {
          setTimeout(printWindow, 150);
        }
      };
      return this;
    }
    
    
    var filelist;
    function init(){
    	getFiles();
    	//alert(files);
    	//filelist=TwoArr(files);
    	//docReady();
    }



    function docReady() {
     	$("div#content").html("");
    	if(filelist.length==0){
    		layer.alert("打印结束!", {icon: 1});
    		window.close();
        	return;
      	}
    	
      	var file=filelist.shift();

     	var type=file[0].type;
      	if(type=='pic'){
        	 var ic = imgCounter(file);
    
        	var imglist ="";
        	var i=0;
         	$.each(file,function(name,value){
        		imglist += "<img class=\"for_print\" style=\"width:100%\" src=\"" + this.url + "\"></img><br class=\"paging\" />";
        	}); 
      	var contentDiv = $("div#content").html(imglist);
      	$("img.for_print").attr("onload", ic.increase);
      }else{
    	 var pdfUrl= file[0].url;
    	  $("#printIframe").prop("src",pdfUrl);
    	  $("#printIframe").load(function(){
    		  setTimeout(doPrintPdf,5000);
    	  });
    	  
      }

     
    }
    
  //点击打印按钮，触发事件】
    function doPrintPdf(){
     $("#printIframe")[0].contentWindow.print();
       //延迟阻塞
       setTimeout(function(){alert("打印下一份数据!")},1000);
       //调用打印
       setTimeout(docReady, 1500);
       
    }
  
  

  
  
   

    function allLoadOK() {
      console.log("all load OK");
    }

    $(document).ready(init);
    $(window).bind("load", allLoadOK);
    $("div#content").change(function() {
      console.log("div change");
    });
   
    </script>
  </head>
  <body>
  	<input type="hidden" id="medicalOnlyId" value="${medicalOnlyId}"/>
  	<input type="hidden" id="applyReason" value="${applyReason}"/>
   <iframe id="printIframe" style="display:none;width:595px;height:842px"></iframe> 
  <div id="content" style="width:100%; display:block">
    </div> 
   

    
  </body>
</html>
